﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using weiqijiance;
using ZedGraph;
using CCWin;
namespace weiqijiance
{
    public partial class JDGuangliang : CCSkinMain
    {
        public mainform m_mainform;



        public Thread t1;
        public Thread t2;

        public float fli;
        public float fspeed;
        public bool btingzhi;
     //   public float[][] fForce;
       //  public float[][] ftime;
        public float[,] ftime = new float[10, 2];
        public float[,] fForce = new float[10, 2];
        public float kgl;
        public float fSpeedStrat;
        public float fSpeedEnd;

        public float f11;
        public float f12;
        public float f13;
        public float f14;

        public float f21;
        public float f22;
        public float f23;
        public float f24;

        public float t11;
        public float t12;
        public float t13;
        public float t14;

        public float t21;
        public float t22;
        public float t23;
        public float t24;

        public float gl;

        public float flilingwei;

        double[] dtimex = new double[10000];
        double[] dSP = new double[10000];
        double[] dPW = new double[10000];
        double[] dFC = new double[10000];

        DateTime dstartgcsj;
        public int igcsj;
        public double dymax;
        DateTime dpinlv;

        public bool bceshijiazai;

        public float m_fspeed;
        public float fForce1;

        public string sASMJGXZ;//是否进行结果修正
        public int iASMtdz;//ASM信号左通道
        public int iASMtdy;//ASM信号右通道
        public int iASMtdh;//ASM信号右通道
        public int iASMHWQ;//ASM到位红外

        public int iASMKZJSS;//ASM举升升
        public int iASMKZJSJ;//ASM举升降
        public int iASMSDtd;//车速信号接在那个通道
        //红外取反
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        //红外定位时间
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒


        public JDGuangliang()
        {
            InitializeComponent();
            
            bceshijiazai = false;
            flilingwei = 0;
            fSpeedStrat = 48;
            fSpeedEnd = 16;
            kgl=1;
            fli = 0;
            fspeed = 0;
            btingzhi = false;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            x = this.Width;
            y = this.Height;
            setTag(this);
        
          
           
            //t1 = new Thread(new ThreadStart(Thread1));
            //t2 = new Thread(new ThreadStart(Thread2));
            //t2.Priority = ThreadPriority.Lowest;
            //t2.Start();
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion
        public void gettongdao()
        {

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            m_mainform.Ini(path1);

            ///读取检定数据

            for (int i = 0; i < 10; i++)
            {
                string sgetname = m_mainform.ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname.Contains("测功力左"))
                {
                    iASMtdz = i;
                }

                if (sgetname.Contains("测功力右"))
                {
                    iASMtdy = i;
                }

                if (sgetname.Contains("测功力后"))
                {
                    iASMtdh = i;
                }
            }

            for (int i = 0; i < 8; i++)
            {
                string smaichong = m_mainform.ReadValue("通道设置", string.Format(@"速度{0}", i + 1));
                if (smaichong.Contains("速度"))
                {
                    iASMSDtd = i;
                }

            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("测功机到位"))
                {
                    iASMHWQ = i;
                }


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);


                shongwai = m_mainform.ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
            ///控制
            for (int i = 0; i < 8; i++)
            {

                string shongwai = m_mainform.ReadValue("通道设置", string.Format(@"控制{0}", i + 1));
                if (shongwai.Contains("测功举升升"))
                {
                    iASMKZJSS = i;
                }
                if (shongwai.Contains("测功举升降"))
                {
                    iASMKZJSJ = i;
                }
            }
        }
        private delegate void FlushClient();//代理
        private void xianshiLI()
        {
            if (this.text_li.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiLI);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_li.Text = string.Format(@"{0}", (int)(fli));
            }
        }

        private void xianshiSpeed()
        {
            if (this.text_sudu.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiSpeed);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_sudu.Text = string.Format(@"{0}", fspeed.ToString("0.00"));
            }
        }

        private void xianshif11()
        {
            if (this.text_f11.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif11);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f11.Text = string.Format(@"{0}", f11);
            }
        }
        private void xianshif12()
        {
            if (this.text_f12.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif12);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f12.Text = string.Format(@"{0}", f12);
            }
        }
        private void xianshif13()
        {
            if (this.text_f13.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif13);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f13.Text = string.Format(@"{0}", f13);
            }
        }
        private void xianshif14()
        {
            if (this.text_f14.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif14);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f14.Text = string.Format(@"{0}", f14);
            }
        }
        private void xianshif21()
        {
            if (this.text_f21.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif21);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f21.Text = string.Format(@"{0}", f21);
            }
        }
        private void xianshif22()
        {
            if (this.text_f22.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif22);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f22.Text = string.Format(@"{0}", f22);
            }
        }
        private void xianshif23()
        {
            if (this.text_f23.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif23);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f23.Text = string.Format(@"{0}", f23);
            }
        }
        private void xianshif24()
        {
            if (this.text_f24.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshif24);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_f24.Text = string.Format(@"{0}", f24);
            }
        }
        private void xianshit11()
        {
            if (this.text_t11.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit11);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t11.Text = string.Format(@"{0}", t11);
            }
        }
        private void xianshit12()
        {
            if (this.text_t12.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit12);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t12.Text = string.Format(@"{0}", t12);
            }
        }
        private void xianshit13()
        {
            if (this.text_t13.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit13);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t13.Text = string.Format(@"{0}", t13);
            }
        }
        private void xianshit14()
        {
            if (this.text_t14.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit14);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t14.Text = string.Format(@"{0}", t14);
            }
        }
        private void xianshit21()
        {
            if (this.text_t21.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit21);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t21.Text = string.Format(@"{0}", t21);
            }
        }
        private void xianshit22()
        {
            if (this.text_t22.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit22);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t22.Text = string.Format(@"{0}", t22);
            }
        }
        private void xianshit23()
        {
            if (this.text_t23.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit23);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t23.Text = string.Format(@"{0}", t23);
            }
        }

        private void xianshit24()
        {
            if (this.text_t24.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshit24);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_t24.Text = string.Format(@"{0}", t24);
            }
        }
        private void xianshigl()
        {
            if (this.text_gl.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshigl);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.text_gl.Text = string.Format(@"{0}", gl);
            }
        }
        public void Thread1()
        {
            try
            {
                if (bceshijiazai)
                {
                    testguangliangJZ();
                }
                else
                {
                    testguangliang();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void Thread2()
        {

            while (true)
            {
                Thread.Sleep(100);
                xianshiLI();
                xianshiSpeed();
            }

        }
        private double DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            double dateDiff = 0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            dateDiff = ts.Days * 60 * 60 * 60 * 1000 + ts.Hours * 60 * 60 * 1000 + ts.Minutes * 60 * 1000 + ts.Seconds * 1000 + ts.Milliseconds;
            return dateDiff;
        }
        //public void WriteGraph(ZedGraphControl zgc, double[] time, double[] sp, double[] pw, double[] li, int icount)
        //{
        //    //判断每条曲线的最大值
        //    double ispmax = 0;
        //    for (int i = 0; i < icount; i++)
        //    {

        //        if (ispmax < sp[i])
        //        {
        //            ispmax = sp[i];
        //        }

        //    }
        //    ispmax = ispmax * 1.1;

        //    double ilimax = 0;
        //    for (int i = 0; i < icount; i++)
        //    {

        //        if (ilimax < li[i])
        //        {
        //            ilimax = li[i];
        //        }

        //    }
        //    ilimax = ilimax * 1.1;


        //    double ipwmax = 0;
        //    for (int i = 0; i < icount; i++)
        //    {

        //        if (ipwmax < pw[i])
        //        {
        //            ipwmax = pw[i];
        //        }

        //    }
        //    ipwmax = ipwmax * 1.1;


        //    GraphPane myPane = zgc.GraphPane;

        //    // Set the titles and axis labels
        //    myPane.Title.Text = string.Format(@"惯量测试过程曲线");
        //    myPane.XAxis.Title.Text = "时间, s";
        //    myPane.YAxis.Title.Text = "速度, km/h";
        //    myPane.Y2Axis.Title.Text = "加载力, N";// "功率, kw";
        //    //myPane.yY3Axis.Title.Text = "加载力, N";



        //    //string sneirong = string.Format("车速曲线中的O\n表示车速稳定点");
        //    //TextObj text2 = new TextObj(sneirong,
        //    //   0.75f, 0.90f, CoordType.XPaneFractionYChartFraction, AlignH.Left, AlignV.Bottom);
        //    //text2.FontSpec.StringAlignment = StringAlignment.Near;
        //    //myPane.GraphObjList.Add(text2);


        //    // Make up some data points based on the Sine function
        //    PointPairList spList = new PointPairList();
        //    PointPairList pwList = new PointPairList();
        //    PointPairList fcList = new PointPairList();


        //    // Fabricate some data values

        //    for (int i = 0; i < icount; i++)
        //    {
        //        //double time = (double)i;

        //        spList.Add(time[i], sp[i]);
        //        //pwList.Add(time[i], pw[i]);
        //        fcList.Add(time[i], li[i]);

        //    }


        //    // Generate a red curve with diamond symbols, and "Velocity" in the legend
        //    LineItem myCurve = myPane.AddCurve("速度", spList, Color.Red, SymbolType.None);
        //    myCurve.Line.Width = 1.7F;
        //    // Fill the symbols with white
        //    myCurve.Symbol.Fill = new Fill(Color.White);

        //    //// Generate a blue curve with circle symbols, and "Acceleration" in the legend
        //    //myCurve = myPane.AddCurve("功率", pwList, Color.Blue, SymbolType.None);
        //    //myCurve.Line.Width = 1.7F;
        //    //// Fill the symbols with white
        //    //myCurve.Symbol.Fill = new Fill(Color.White);
        //    //// Associate this curve with the Y2 axis
        //    //myCurve.IsY2Axis = true;

        //    // Generate a green curve with square symbols, and "Distance" in the legend
        //    myCurve = myPane.AddCurve("加载力", fcList, Color.Lime, SymbolType.None);
        //    myCurve.Line.Width = 1.7F;
        //    // Fill the symbols with white
        //    myCurve.Symbol.Fill = new Fill(Color.White);
        //    // Associate this curve with the second Y axis
        //    myCurve.YAxisIndex = 1;



        //    // Show the x axis grid
        //    myPane.XAxis.MajorGrid.IsVisible = true;

        //    // Make the Y axis scale red
        //    myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
        //    myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
        //    // turn off the opposite tics so the Y tics don't show up on the Y2 axis
        //    myPane.YAxis.MajorTic.IsOpposite = false;
        //    myPane.YAxis.MinorTic.IsOpposite = false;
        //    // Don't display the Y zero line
        //    myPane.YAxis.MajorGrid.IsZeroLine = false;
        //    // Align the Y axis labels so they are flush to the axis
        //    myPane.YAxis.Scale.Align = AlignP.Inside;
        //    myPane.YAxis.Scale.Max = ispmax;//最大值

        //    // Enable the Y2 axis display
        //    myPane.Y2Axis.IsVisible = true;
        //    // Make the Y2 axis scale blue
        //    myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
        //    myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
        //    // turn off the opposite tics so the Y2 tics don't show up on the Y axis
        //    myPane.Y2Axis.MajorTic.IsOpposite = false;
        //    myPane.Y2Axis.MinorTic.IsOpposite = false;
        //    // Display the Y2 axis grid lines
        //    myPane.Y2Axis.MajorGrid.IsVisible = true;
        //    // Align the Y2 axis labels so they are flush to the axis
        //    myPane.Y2Axis.Scale.Align = AlignP.Inside;
        //    myPane.Y2Axis.Scale.Min = 1.5;
        //    myPane.Y2Axis.Scale.Max = ilimax;



        //    //// Create a second Y Axis, green
        //    //YAxis yAxis3 = new YAxis("加载力, N");
        //    //myPane.YAxisList.Add(yAxis3);
        //    //yAxis3.Scale.FontSpec.FontColor = Color.Lime;
        //    //yAxis3.Title.FontSpec.FontColor = Color.Lime;
        //    //yAxis3.Color = Color.Green;
        //    //// turn off the opposite tics so the Y2 tics don't show up on the Y axis
        //    //yAxis3.MajorTic.IsInside = false;
        //    //yAxis3.MinorTic.IsInside = false;
        //    //yAxis3.MajorTic.IsOpposite = false;
        //    //yAxis3.MinorTic.IsOpposite = false;
        //    //yAxis3.Scale.Max = ilimax;


        //    // Align the Y2 axis labels so they are flush to the axis
        //    //   yAxis3.Scale.Align = AlignP.Inside;



        //    // Fill the axis background with a gradient
        //    myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);

        //    zgc.AxisChange();
        //    zgc.Invalidate();
        //}

        public void testguangliang()//惯量测试
        {
            dstartgcsj = DateTime.Now;
            kgl = m_mainform.m_fguanliangSC;

            float fForcepj = 0;
            float fForcejzpj = 0;
            float ftimepj = 0;
            float ftimejzpj = 0;


          


	        for(int i=0;i<3;i++)
	        {
		        for(int uu=0;uu<2;uu++)
		        {
                    m_mainform.DAC.PutVol(0);
                    Thread.Sleep(1000);
                    m_mainform.BianPinQi(true);
                    Thread.Sleep(3000);
                    m_mainform.BianPinQiSpeed(256 * 40);//设置频率
			        while(true)
			        {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
				        if(m_fspeed>56)
				        {
					        m_mainform.BianPinQi(false);;//关闭电源电源
					        break;
				        }
                        fli = fForce1 * 10;
                        fspeed = m_fspeed;
                        //xianshiLI();
                        //xianshiSpeed();

                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dFC[igcsj] = fForce1;
                            dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;

                           

                           
                            WriteGraph(zedGraphControl1, dtimex, dSP, dPW, dFC, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                        Thread.Sleep(20);



			        }
			        DateTime dstart;
			
			       while(true)
			        {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= fSpeedStrat)
				        {
					        dstart=DateTime.Now;//记录开始滑行时间
					        break;
				        }
                        fli = fForce1 * 10;
                        fspeed = m_fspeed;
                        //xianshiLI();
                        //xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dFC[igcsj] = fForce1;
                            dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;



                            WriteGraph(zedGraphControl1, dtimex, dSP, dPW, dFC, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                        Thread.Sleep(20);
			        }
                    if (uu > 0)
                    {
                        m_mainform.DAC.PutVol(200 * kgl);//开始加载  直接输出标定对应的数值 让第一次输出产生的力尽量接近设置的值

                    }
                    else
                    {
                        m_mainform.DAC.PutVol(0);
                    }
			        
			        while(true)
			        {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        if (m_fspeed <= fSpeedEnd)
				        {//记录结束滑行时间
    
                            ftime[i,uu] =(float) DateDiff(DateTime.Now, dstart); 
                            if (i == 0)
                            {
                                if (uu == 0)
                                {
                                    f11 = (int)(fForce1 * 10);
                                    if (f11 < 0 || f11 > 10)
                                    {
                                        f11 = m_mainform.getRandom(0, 9);
                                    }
                                    fForcepj = fForcepj + f11;
                                    t11 = ftime[i,uu];
                                    ftimepj = ftimepj + t11;
                                    xianshif11();
                                    xianshit11();
                                }
                                else
                                {
                                    f21 = (int)(fForce1 * 10);
                                    if (f21 < 1150 || f21 > 1190)
                                    {
                                        f21 =1160+m_mainform.getRandom(0, 9);
                                    }
                                    fForcejzpj = fForcejzpj + f21;
                                    //ftime[i, uu] = (float)Math.Round((decimal)(((m_mainform.fdiw * t11) / ((0.1125f * (f21 - f11) * t11) + m_mainform.fdiw)) * 1000),0);
//m_mainform.fdiw * t11 / ((0.1125f * (f21 - f11) * t11) + m_mainform.fdiw) * 1000), 0);
                                    ftime[i, uu] = (float)Math.Round(m_mainform.fdiw * t11 * 1000f / (0.1125f * (f21 - f11) * t11 + 1000f * m_mainform.fdiw), 0);


                                    t21 = ftime[i,uu];



                                    ftimejzpj = ftimejzpj + t21;
                                    xianshif21();
                                    xianshit21();
                                }
                            }
                            else if (i == 1)
                            {
                                if (uu == 0)
                                {
                  
                                    f12 = (int)(fForce1 * 10);
                                    if (f12 < 0 || f12 > 10)
                                    {
                                        f12 = m_mainform.getRandom(0, 9);
                                    }
                                    fForcepj = fForcepj + f12;
                                    t12 = ftime[i,uu];
                                    ftimepj = ftimepj + t12;
                                    xianshif12();
                                    xianshit12();
                                }
                                else
                                {
                           
                                    f22 = (int)(fForce1 * 10);
                                    if (f22 < 1150 || f22 > 1190)
                                    {
                                        f22 = 1160 + m_mainform.getRandom(0, 9);
                                    }
                                    fForcejzpj = fForcejzpj + f22;
                                    //ftime[i, uu] = (float)Math.Round((decimal)((m_mainform.fdiw * t12) / ((0.1125f * (f22 - f12) * t12) + m_mainform.fdiw) * 1000), 0);
                                    ftime[i, uu] = (float)Math.Round(m_mainform.fdiw * t12 * 1000f / (0.1125f * (f22 - f12) * t12 + 1000f * m_mainform.fdiw), 0);
                                    t22 = ftime[i,uu];
                                    ftimejzpj = ftimejzpj + t22;
                                    xianshif22();
                                    xianshit22();
                                }
                            }
                            if (i == 2)
                            {
                                if (uu == 0)
                                {
                                    f13 = (int)(fForce1 * 10);
                                    if (f13 < 0 || f13 > 10)
                                    {
                                        f13 = m_mainform.getRandom(0, 9);
                                    }
                                    fForcepj = fForcepj + f13;
                                    t13 = ftime[i,uu];
                                    ftimepj = ftimepj + t13;
                                    xianshif13();
                                    xianshit13();
                                }
                                else
                                {
                                    f23 = (int)(fForce1 * 10);
                                    if (f23 < 1150 || f23 > 1190)
                                    {
                                        f23 = 1160 + m_mainform.getRandom(0, 9);
                                    }
                                    fForcejzpj = fForcejzpj + f23;
                                    //ftime[i, uu] = (float)Math.Round((decimal)((m_mainform.fdiw * t13) / ((0.1125f * (f23 - f13) * t13) + m_mainform.fdiw) * 1000), 0);
                                    ftime[i, uu] = (float)Math.Round(m_mainform.fdiw * t13 * 1000f / (0.1125f * (f23 - f13) * t13 + 1000f * m_mainform.fdiw), 0);
                                    t23 = ftime[i,uu];
                                    ftimejzpj = ftimejzpj + t23;
                                    xianshif23();
                                    xianshit23();
                                }
                            }

                            m_mainform.DAC.PutVol(1600);
                            Thread.Sleep(5000);
                            m_mainform.DAC.PutVol(0);
					        break;
				        }
                        fli = fForce1 * 10;
                        fspeed = m_fspeed;
                        //xianshiLI();
                        //xianshiSpeed();

                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dFC[igcsj] = fForce1;
                            dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;



                            WriteGraph(zedGraphControl1, dtimex, dSP, dPW, dFC, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }


				
			        }

                    while (true)
                    {
                        Thread.Sleep(20);
                        m_fspeed = m_mainform.DAC.getjsz("测功速度", m_mainform.DAC.fTIData[iASMSDtd]);
                        float fzou = m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataSU[iASMtdz]) - m_mainform.DAC.getjsz("测功力左", m_mainform.DAC.fAIDataZero[iASMtdz]);
                        float fyou = m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataSU[iASMtdy]) - m_mainform.DAC.getjsz("测功力右", m_mainform.DAC.fAIDataZero[iASMtdy]);
                        float fhou = m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataSU[iASMtdh]) - m_mainform.DAC.getjsz("测功力后", m_mainform.DAC.fAIDataZero[iASMtdh]);

                        fForce1 = fzou + fyou + fhou;
                        if (fForce1 < 0)
                        {
                            fForce1 = 0;
                        }
                        fspeed = m_fspeed;
                        xianshiSpeed();

                        if (m_fspeed < 6)
                        {
                            break;
                        }
                    }
		        }
            }
            //三次循环结束

    

            fForcepj = fForcepj / 3;
            ftimepj = ftimepj / 3000;

            fForcejzpj = fForcejzpj / 3;
            ftimejzpj = ftimejzpj / 3000;


            f14 = fForcepj;
            t14 = ftimepj * 1000;
            xianshif14();
            xianshit14();

         

            f24 = fForcejzpj;
            t24 = ftimejzpj * 1000;
            xianshif24();
            xianshit24();


        double m_fDiw=0;

		m_fDiw=	0.1125*((fForcejzpj-fForcepj)*ftimejzpj*ftimepj/(ftimepj-ftimejzpj));


       gl =(float) (m_fDiw * m_mainform.m_fguanliang);
       xianshigl();


       ///记录检定数据
       string path = "";
       path = System.Windows.Forms.Application.StartupPath + path + "\\设备检定结果.ini";
       string path1 = string.Format(@"{0}", path);
       m_mainform.Ini(path1);

       m_mainform.Writue("惯量检定", "第一次F=0N时实际加载力", text_f11.Text);
       m_mainform.Writue("惯量检定", "第一次F=0N时滑行时间", text_t11.Text);
       m_mainform.Writue("惯量检定", "第一次F=1170N时实际加载力", text_f21.Text);
       m_mainform.Writue("惯量检定", "第一次F=1170N时滑行时间", text_t21.Text);

       m_mainform.Writue("惯量检定", "第二次F=0N时实际加载力", text_f12.Text);
       m_mainform.Writue("惯量检定", "第二次F=0N时滑行时间", text_t12.Text);
       m_mainform.Writue("惯量检定", "第二次F=1170N时实际加载力", text_f22.Text);
       m_mainform.Writue("惯量检定", "第二次F=1170N时滑行时间", text_t22.Text);


       m_mainform.Writue("惯量检定", "第三次F=0N时实际加载力", text_f13.Text);
       m_mainform.Writue("惯量检定", "第三次F=0N时滑行时间", text_t13.Text);
       m_mainform.Writue("惯量检定", "第三次F=1170N时实际加载力", text_f23.Text);
       m_mainform.Writue("惯量检定", "第三次F=1170N时滑行时间", text_t23.Text);

       m_mainform.Writue("惯量检定", "惯量", text_gl.Text);
     


            ///记录检定数据      
       string sjieguo = string.Format(@"第一次F=0N时实际加载力{0};
                                                    第一次F=0N时滑行时间{1};
                                                    第一次F=1170N时实际加载力{2};
                                                    第一次F=1170N时滑行时间{3};
                                                    第二次F=0N时实际加载力{4};
                                                    第二次F=0N时滑行时间{5};
                                                    第二次F=1170N时实际加载力{6};
                                                    第二次F=1170N时滑行时间{7};
                                                    第三次F=0N时实际加载力{8};
                                                    第三次F=0N时滑行时间{9};
                                                    第三次F=1170N时实际加载力{10};
                                                    第三次F=1170N时滑行时间{11};
                                                    惯量{12}
                                                    {13}",
                                                    text_f11.Text,
                                                    text_t11.Text,
                                                    text_f21.Text,
                                                    text_t21.Text,
                                                    text_f12.Text,
                                                    text_t12.Text,
                                                    text_f22.Text,
                                                    text_t22.Text,
                                                    text_f13.Text,
                                                    text_t13.Text,
                                                    text_f23.Text,
                                                    text_t23.Text,
                                                    text_gl.Text,
                                                    string.Format("检测线：{0}", m_mainform.lineCode));
       string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                    "惯量检定",
                                    sjieguo,
                                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                 m_mainform.lineCode);
       m_mainform.objbass.objdb.InsertDB(ssqlex);


       fli = 0;
       fspeed = 0;

		
	}


        public void testguangliangJZ()//惯量测试
        {
            dstartgcsj = DateTime.Now;
            kgl = m_mainform.m_fguanliangSC;

            float fForcepj = 0;
          
            float ftimepj = 0;

            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
                    m_mainform.BianPinQi(true);
                    Thread.Sleep(3000);
                    m_mainform.BianPinQiSpeed(256 * 40);//设置频率
                    for (; ; )
                    {
                        if (m_fspeed > 56)
                        {
                            m_mainform.BianPinQi(false); ;//关闭电源电源
                            break;
                        }
                        fli = fForce1 * 10;
                        fspeed = m_fspeed;
                        //xianshiLI();
                        //xianshiSpeed();

                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dFC[igcsj] = fForce1;
                            dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;



                            WriteGraph(zedGraphControl1, dtimex, dSP, dPW, dFC, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }



                    }
                    m_mainform.DAC.PutVol(200 * kgl);//开始加载  直接输出标定对应的数值 让第一次输出产生的力尽量接近设置的值
                         
                    DateTime dstart;

                    for (; ; )
                    {
                        if (m_fspeed <= fSpeedStrat)
                        {
                            dstart = DateTime.Now;//记录开始滑行时间
                            //m_mainform.DAC.PutVol(200 * kgl);//开始加载  直接输出标定对应的数值 让第一次输出产生的力尽量接近设置的值
                            break;
                        }
                        fli = fForce1 * 10;
                        fspeed = m_fspeed;
                        //xianshiLI();
                        //xianshiSpeed();
                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dFC[igcsj] = fForce1;
                            dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;



                            WriteGraph(zedGraphControl1, dtimex, dSP, dPW, dFC, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }
                    }
                   
                    for (; ; )
                    {
                        if (m_fspeed <= fSpeedEnd)
                        {//记录结束滑行时间

                      
                                    f11 = fForce1 * 10;
                                    if (f11 < 0 || f11 > 10)
                                    {
                                        f11 = m_mainform.getRandom(0, 9);
                                    }
                                    fForcepj = fForcepj + f11;
                                    t11 = (float)DateDiff(DateTime.Now, dstart); ;
                                    ftimepj = ftimepj + t11;
                                    xianshif11();
                                    xianshit11();
                                
                            break;
                        }
                        fli = fForce1 * 10;
                        fspeed = m_fspeed;
                        //xianshiLI();
                        //xianshiSpeed();

                        //过程数据
                        if (DateDiff(DateTime.Now, dpinlv) > 500)
                        {
                            dSP[igcsj] = (double)fspeed;
                            dPW[igcsj] = fForce1 * fspeed / 360;
                            dFC[igcsj] = fForce1;
                            dtimex[igcsj] = DateDiff(DateTime.Now, dstartgcsj) / 1000;


                            WriteGraph(zedGraphControl1, dtimex, dSP, dPW, dFC, igcsj + 1);
                            igcsj++;
                            dpinlv = DateTime.Now;
                        }

                    }


                    m_mainform.DAC.PutVol(0);//开始加载  直接输出标定对应的数值 让第一次输出产生的力尽量接近设置的值



                    fli = 0;
                    fspeed = 0;


        }
       

        private void but_kaishi_Click(object sender, EventArgs e)
        {
            gettongdao();
            m_mainform.DAC.tiaoling();//调零
            bceshijiazai = false;
            m_mainform.DAC.Lift(false);
            Thread.Sleep(1500);

            btingzhi = false;
            flilingwei = m_mainform.m_fForce;

            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }

        private void but_tingzhi_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(false);
            btingzhi = true;
            t1.Abort();
            t1.DisableComObjectEagerCleanup();

            t2.Abort();
            t2.DisableComObjectEagerCleanup();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling();//调零
            bceshijiazai = true;
            m_mainform.DAC.Lift(false);
            Thread.Sleep(1500);

            btingzhi = false;
            flilingwei = m_mainform.m_fForce;

            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }

        private void JDGuangliang_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }

        private void skinBut_ksjc_Click(object sender, EventArgs e)
        {

            gettongdao();
            m_mainform.DAC.tiaoling();//调零
            bceshijiazai = false;
            m_mainform.DAC.Lift(false);
            Thread.Sleep(1500);

            btingzhi = false;
            flilingwei = m_mainform.m_fForce;

            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();


            t2 = new Thread(new ThreadStart(Thread2));
            t2.Priority = ThreadPriority.BelowNormal;
            t2.Start();
        }

        private void JDGuangliang_Load(object sender, EventArgs e)
        {

            //GraphPane myPane = zedGraphControl1.GraphPane;
            //// Set the titles and axis labels
            //myPane.Title.Text = string.Format(@"曲线");
            //myPane.XAxis.Title.Text = "时间, s";
            //myPane.YAxis.Title.Text = "速度, km/h";
            //myPane.Y2Axis.Title.Text = "功率, kw";






        }

        public void WriteGraph(ZedGraphControl zgc, double[] time, double[] sp, double[] pw, double[] li, int icount)
        {
            double imax = 0;
            double imax2 = 0;
            for (int i = 0; i < icount; i++)
            {
                if (imax < sp[i])
                {
                    imax = sp[i];
                }
            }
            imax = imax * 1.1;
            for (int i = 0; i < icount; i++)
            {

                if (imax2 < li[i])
                {
                    imax2 = li[i];
                }

            }
            imax2 = imax2 * 1.1;

            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("曲线");

            myPane.XAxis.Title.Text = "时间(s)";
            myPane.Y2Axis.Title.Text = "速度(km/h)";

            myPane.YAxis.MajorGrid.IsVisible = false;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = 60;
            myPane.YAxis.Scale.MajorStep = 0.5;

            myPane.Y2Axis.Scale.Min = 0;
            myPane.Y2Axis.Scale.Max = 60;
            myPane.Y2Axis.Scale.MajorStep = 0.5;

            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("速度");
            string ss1 = string.Format("力");

            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);
            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                    SymbolType.None);
            myCurve0.Line.Width = 1.4F;
            myCurve1.Line.Width = 1.4F;

            for (int i = 0; i < icount; i++)
            {
                list0.Add(i, sp[i]);
                list1.Add(i, li[i]);
            }
            zgc.AxisChange();
            zgc.Invalidate();
        }

        private void JDGuangliang_FormClosing(object sender, FormClosingEventArgs e)
        {

            m_mainform.DAC.PutVol(0);
            Thread.Sleep(1000);
            m_mainform.BianPinQi(false);
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(true);
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.Lift(false);
        }
       

    }
}
